package org.example.petitionplatformsystem.utils;

import org.example.petitionplatformsystem.dao.model.Employees;
import org.example.petitionplatformsystem.dao.model.PetitionEvents;

import java.util.List;

public class EventTransferUtil {
    public static PetitionEvents ETU(PetitionEvents petitionEvents, List<Employees> employeesList) {
        // 找到工作量最少的员工
        Employees leastBusyEmployee = findLeastBusyEmployee(employeesList);

        // 如果找到员工，分配事件
        if (leastBusyEmployee != null) {
            petitionEvents.setWorkId(leastBusyEmployee.getWorkID());
            petitionEvents.setStatus(3L);
            // 更新员工工作量
            leastBusyEmployee.setWork_Num(leastBusyEmployee.getWork_Num() + 1);
        } else {
            System.out.println("没有可用的员工");
        }

        return petitionEvents;
    }

    // 辅助函数：找到工作量最少的员工
    private static Employees findLeastBusyEmployee(List<Employees> employees) {
        if (employees.isEmpty()) {
            return null; // 没有员工
        }
        Employees leastBusyEmployee = employees.get(0);
        int minWorkLoad = leastBusyEmployee.getWork_Num();
        for (int i = 1; i < employees.size(); i++) {
            Employees currentEmployee = employees.get(i);
            int currentWorkLoad = currentEmployee.getWork_Num();
            if (currentWorkLoad < minWorkLoad) {
                leastBusyEmployee = currentEmployee;
                minWorkLoad = currentWorkLoad;
            }
        }
        return leastBusyEmployee;
    }
}
